﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using umbraco;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;

namespace CodeGecko.Modules.Umbraco.AppCodeEditor
{
	public class AppCodeTree : FileSystemTree
	{
		public AppCodeTree(String application) : base(application) { }

		protected override void CreateAllowedActions(ref List<IAction> actions)
		{
			actions.Clear();
			actions.Add(ActionRefresh.Instance);
		}

		protected override void CreateRootNodeActions(ref List<IAction> actions)
		{
			actions.Clear();
			actions.Add(ActionRefresh.Instance);
		}

		protected override void CreateRootNode(ref XmlTreeNode rootNode)
		{
			rootNode.Text = "App_Code";
			rootNode.Icon = "developerMacro.gif";
			rootNode.OpenIcon = "developerMacro.gif";
			rootNode.NodeID = "init";
			rootNode.NodeType = String.Concat(rootNode.NodeID, this.TreeAlias);
		}

		protected override string FilePath
		{
			get { return String.Concat(GlobalSettings.FullpathToRoot, "/App_Code/"); }
		}

		protected override string FileSearchPattern
		{
			get { return "*.*"; }
		}

		protected override void OnRenderFileNode(ref XmlTreeNode xNode)
		{
			xNode.Action = xNode.Action.Replace("openFile", "openAppCodeEditor");
			xNode.Icon = "settingsScript.gif";
			xNode.OpenIcon = "settingsScript.gif";
		}

		protected override void OnRenderFolderNode(ref XmlTreeNode xNode)
		{
			xNode.Menu = new List<IAction>(new IAction[] { ActionRefresh.Instance });
			xNode.NodeType = "appCodeFolder";
		}

		public override void RenderJS(ref StringBuilder Javascript)
		{
			Javascript.Append(@"
				function openAppCodeEditor(file) {
					parent.right.document.location.href = 'developer/editCodeFile.aspx?file=/App_Code/' + file;
				}
			");
		}
	}
}
